package com.yj.service;

import java.util.List;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yj.dao.resource.ResourceDao;
import com.yj.entity.InstanceSaveReq;
import com.yj.exception.BusinessException;
import com.yj.exception.BusinessExceptionEnums;
import com.yj.exception.EntityException;
import com.yj.model.comm.ResultUtil;
import com.yj.model.resource.ResourceInfo;
import com.yj.model.resource.ResourceInfoQueryBO;

@Service
@Transactional(rollbackFor = Exception.class)
public class ResourceService extends BaseEntityService<ResourceInfo> {

	@Value("${entityType.resource}")
	private int entityType;

	@Autowired
	private ResourceDao resourceDao;

	@Override
	protected int getEntityTypeCode() {
		return entityType;
	}

	@Override
	protected String saveInstanceInfo(ResourceInfo resourceInfo) {
		String id = getSequence();
		resourceInfo.setInstanceId(id);
		resourceDao.saveInstanceInfo(resourceInfo);
		return id;
	}

	@Override
	protected int updateInstanceInfo(ResourceInfo resourceInfo) {
		return resourceDao.updateInstanceInfo(resourceInfo);
	}

	@Override
	protected ResourceInfo getInstanceInfo(String instanceId) {
		return resourceDao.getInstanceInfo(instanceId);
	}

	public ResultUtil listInstances(ResourceInfoQueryBO resourceInfoQueryBO) throws BusinessException {
		ResultUtil resultUtil = new ResultUtil();
		if (0 == resourceInfoQueryBO.getOwnerId()) {
			throw new BusinessException(BusinessExceptionEnums.NULL_OWNERID);
		}
		int total = resourceDao.pageCount(resourceInfoQueryBO);
		List<ResourceInfoQueryBO> list = null;
		if (total > 0) {
			list = resourceDao.pageQuery(resourceInfoQueryBO);
		}
		resultUtil.setTotal(total);
		resultUtil.setRows(list);
		return resultUtil;
	}

	/**
	 * 修改的情况下,要判断该资源能否被修改 flag=0 可被修改 flag>0 不可被修改
	 */
	private int canModify(ResourceInfo instanceInfo) throws BusinessException {
		int flag = 0;
		String instanceId = instanceInfo.getInstanceId();
		if (StringUtils.isNotBlank(instanceId)) {
			flag = resourceDao.canModify(instanceInfo);
		}
		if (flag > 0) {
			throw new BusinessException(BusinessExceptionEnums.RESOURCE_CANMODIFY_ERR);
		}
		return flag;
	}

	public void saveResource(InstanceSaveReq<ResourceInfo> instanceSaveReq) throws BusinessException, EntityException {
		try {
			ResourceInfo instanceInfo = instanceSaveReq.getInstanceInfo();
			canModify(instanceInfo);
			saveInstance(instanceSaveReq);
		} catch (EntityException e) {
			throw e;
		} catch (BusinessException e) {
			throw e;
		}
	}

	public void changeResourceStatus(ResourceInfo resourceInfo) throws BusinessException, EntityException {
		canModify(resourceInfo);
		changeInstanceStatus(resourceInfo);
	}

	@Override
	protected String getSequence() {
		return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16);
	}
}
